package stockblaster.client;

import java.text.DecimalFormat;

import stockblaster.gen.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MethodHelper extends Activity {
	private static final Class<?> MAIN_SCREEN = SplashActivity.class;
	private static Class<?> fromClass = null;

	// public static final int LOG_OUT = 1;
	// public static final int HELP = 2;
	// public static final int HELP_FIRST = 113;
	// public static final int OBJ_AFTER_HELP = 14;
	// public static final int COMPANY = 15;
	// public static final int FIRST_BUY = 6;
	// public static final int PORTFOLIO = 7;
	// public static final int BUY_MORE = 18;
	// public static final int NEXT_MONTH = 9;
	// public static final int NEWS = 10;
	// public static final int FINAL = 11;

	public static final int SUMMARY = 0;
	// public static int SEARCH = 1;
	public static final int COMPANY_PAGE = 1;
	public static final int FIRST_BUY = 2;
	public static final int PORTFOLIO = 3;
	public static final int MORE_STOCK = 4;
	public static final int NEXT_MONTH = 5;
	public static final int NEWS = 6;
	public static final int FINAL = 7;

	public static final int HELP = 11;
	public static final int LOG_OUT = 13;

	/**
	 * Method to format a double number into two decimal string
	 * 
	 * @param d
	 *            the double to be formatted
	 * @return the String in form of $0.00
	 */
	public static String twoDecimal(double d) {
		DecimalFormat priceFormatter = new DecimalFormat("$#0.00");
		return priceFormatter.format(d);
	}

	/**
	 * Method to set the bread crumbs
	 * 
	 * @param _from
	 *            the activity this method is called from
	 */
	public static void setFromClass(Class<?> _from) {
		fromClass = _from;
	}

	/**
	 * Method to return the fromClass variable
	 * 
	 * @return the activity stored in fromClass
	 */
	public static Class<?> getFromClass() {
		return fromClass;
	}

	/**
	 * Takes an activity, a title, and a message to be displayed on an alert box
	 * 
	 * example usage: "Helper.showAlertBox(ClassName.this, "title", "message")
	 * 
	 * @param a
	 *            Activity that calls this method
	 * @param title
	 *            title of the alert box
	 * @param msg
	 *            message of the alert box
	 */
	public static Dialog showAlertBox(Activity a, String title, String msg) {
		return new AlertDialog.Builder(a).setTitle(title).setMessage(msg)
				.setPositiveButton("OK", null).show();
	}

	/**
	 * Takes an activity and a message. Displays a toast on specified activity
	 * with message.
	 * 
	 * example usage: "Helper.showToast(ClassName.this, "It Works!");
	 * 
	 * @param a
	 *            Activity toast should show up in
	 * @param message
	 *            message for Toast
	 */
	public static void showToast(Activity a, String message) {
		/*
		 * LayoutInflater inflater = a.getLayoutInflater(); View layout =
		 * inflater.inflate(R.layout.toast_layout, (ViewGroup)
		 * a.findViewById(R.id.toast_layout_root));
		 * 
		 * ImageView image = (ImageView) layout.findViewById(R.id.imageView1);
		 * image.setImageResource(R.drawable.scl); //TextView text = (TextView)
		 * layout.findViewById(R.id.text); //text.setText(message); ProgressBar
		 * pBar = (ProgressBar) layout.findViewById(R.id.progressBar1);
		 * 
		 * Toast toast = new Toast(a.getApplicationContext());
		 * toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		 * toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout);
		 * toast.show();
		 */
	}

	/**
	 * Takes an activity and a message. Displays a toast on specified activity
	 * with message. Exactly the same like showToast(), just different picture.
	 * 
	 * example usage: "Helper.showToast(ClassName.this, "It Works!");
	 * 
	 * @param a
	 *            Activity toast should show up in
	 * @param message
	 *            message for Toast
	 */
	public static void showGetHelpToast(Activity a, String message) {
		/*
		 * LayoutInflater inflater = a.getLayoutInflater(); View layout =
		 * inflater.inflate(R.layout.toast_layout, (ViewGroup)
		 * a.findViewById(R.id.toast_layout_root));
		 * 
		 * ImageView image = (ImageView) layout.findViewById(R.id.image);
		 * image.setImageResource(R.drawable.general_android_help1); TextView
		 * text = (TextView) layout.findViewById(R.id.text);
		 * text.setText(message);
		 * 
		 * Toast toast = new Toast(a.getApplicationContext());
		 * toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		 * toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout);
		 * toast.show();
		 */
	}

	/**
	 * This method will show different types of dialog based on the id passed
	 * in.
	 * 
	 * @param a
	 *            Activity you are on
	 * @param to
	 *            Activity to go to
	 * @param dialog_id
	 *            the type of dialog to be shown
	 * @param helpNum
	 *            the help number
	 */
	/*
	 * public static void show_dialogs(final Activity from, final Class<?> to,
	 * final int dialog_id, final int num) { switch (dialog_id) { case LOG_OUT:
	 * 
	 * @SuppressWarnings("unused") AlertDialog adialog = new
	 * AlertDialog.Builder(from) .setTitle("Are you sure you want to log out?")
	 * .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public
	 * void onClick(DialogInterface dialog, int whichButton) { // User clicked
	 * Yes so do some stuff CurrentUser.clearData(); switch_activity(from,
	 * MAIN_SCREEN, false); } }) .setNegativeButton("No", new
	 * DialogInterface.OnClickListener() { public void onClick(DialogInterface
	 * dialog, int whichButton) { // User clicked No so do some stuff
	 * dialog.dismiss(); } }).show(); break; case HELP: final Dialog dialog1 =
	 * new Dialog(from); dialog1.setTitle("Help Tip");
	 * dialog1.setContentView(R.layout.help_dialog); TextView text1 = (TextView)
	 * dialog1.findViewById(R.id.TextView01);
	 * text1.setText(CurrentUser.getHelp(num)); Button button1 = (Button)
	 * dialog1.findViewById(R.id.Button01); button1.setOnClickListener(new
	 * OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { dialog1.dismiss(); } });
	 * dialog1.show(); break; case HELP_FIRST: final Dialog dialog2 = new
	 * Dialog(from); dialog2.setTitle("Help Tip");
	 * dialog2.setContentView(R.layout.help_dialog); TextView text2 = (TextView)
	 * dialog2.findViewById(R.id.TextView01);
	 * text2.setText(CurrentUser.getHelp(num)); Button button2 = (Button)
	 * dialog2.findViewById(R.id.Button01); button2.setOnClickListener(new
	 * OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { dialog2.dismiss();
	 * show_dialogs(from, to, OBJ_AFTER_HELP, num); } }); dialog2.show(); break;
	 * case OBJ_AFTER_HELP: final Dialog dialog3 = new Dialog(from);
	 * dialog3.setTitle("Objective");
	 * dialog3.setContentView(R.layout.help_dialog); TextView text3 = (TextView)
	 * dialog3.findViewById(R.id.TextView01);
	 * text3.setText(CurrentUser.getObjectives(num)); Button button3 = (Button)
	 * dialog3.findViewById(R.id.Button01); button3.setOnClickListener(new
	 * OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { dialog3.dismiss(); } });
	 * dialog3.show(); break; case COMPANY: showHelp(from,
	 * "Time to buy some shares!", CurrentUser.getHelp(num), num); break; case
	 * FIRST_BUY: showHelp(from, "Your first buy!", CurrentUser.getHelp(num),
	 * num); break; case PORTFOLIO: showHelp(from, "Portfolio page",
	 * CurrentUser.getHelp(num), num); break; case BUY_MORE: showHelp(from,
	 * "You have shares in 4 companies now!", CurrentUser.getHelp(num), num);
	 * break; case NEXT_MONTH: showHelp(from,
	 * "You have fast forwarded to the next month!", CurrentUser.getHelp(num),
	 * num); break; case NEWS: showHelp(from, "Here are the news page",
	 * CurrentUser.getHelp(num), num); break; case FINAL: showHelp(from,
	 * "Final tip", CurrentUser.getHelp(num), num); break; } }
	 */

	public static void show_dialogs(final Activity from, final Class<?> to,
			final int num) {
		switch (num) {
		case LOG_OUT:
			@SuppressWarnings("unused")
			AlertDialog adialog = new AlertDialog.Builder(from)
					.setTitle("Are you sure you want to log out?")
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									// User clicked Yes so do some stuff
									CurrentUser.clearData();
									Intent i = new Intent(from, MAIN_SCREEN);
									i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
									i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
									from.startActivity(i);
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									// User clicked No so do some stuff
									dialog.dismiss();
								}
							}).show();
			break;
		case HELP:
			final Dialog dialog1 = new Dialog(from);
			dialog1.setTitle("Help Tip");
			dialog1.setContentView(R.layout.help_dialog);
			TextView text1 = (TextView) dialog1.findViewById(R.id.TextView01);
			text1.setText(CurrentUser.getHelp(num));
			Button button1 = (Button) dialog1.findViewById(R.id.Button01);
			button1.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					dialog1.dismiss();
				}
			});
			dialog1.show();
			break;
		case SUMMARY:
			showHelp(from, "Welcome!", CurrentUser.getHelp(num), num);
			break;
		case COMPANY_PAGE:
			showHelp(from, "Time to buy some shares!",
					CurrentUser.getHelp(num), num);
			break;
		case FIRST_BUY:
			showHelp(from, "Your first buy!", CurrentUser.getHelp(num), num);
			break;
		case PORTFOLIO:
			showHelp(from, "Portfolio page", CurrentUser.getHelp(num), num);
			break;
		case MORE_STOCK:
			showHelp(from, "You have shares in 4 companies now!",
					CurrentUser.getHelp(num), num);
			break;
		case NEXT_MONTH:
			showHelp(from, "You have fast forwarded to the next month!",
					CurrentUser.getHelp(num), num);
			break;
		case NEWS:
			showHelp(from, "Here are the news page", CurrentUser.getHelp(num),
					num);
			break;
		case FINAL:
			showHelp(from, "Final tip", CurrentUser.getHelp(num), num);
			break;
		}
	}

	private static void showHelp(final Activity from, String title,
			String help, final int num) {
		final Dialog dialog2 = new Dialog(from);
		dialog2.setTitle(title);
		dialog2.setContentView(R.layout.help_dialog);
		TextView text2 = (TextView) dialog2.findViewById(R.id.TextView01);
		text2.setText(help);
		Button button2 = (Button) dialog2.findViewById(R.id.Button01);
		button2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog2.dismiss();
				if (num == NEWS)
					return;
				showObjective(from, CurrentUser.getObjectives(num));
			}
		});
		dialog2.show();
	}

	public static void showObjective(Activity from, String objective) {
		final Dialog dialog3 = new Dialog(from);
		dialog3.setTitle("Objectives");
		dialog3.setContentView(R.layout.help_dialog);
		TextView text3 = (TextView) dialog3.findViewById(R.id.TextView01);
		text3.setText(objective);
		Button button3 = (Button) dialog3.findViewById(R.id.Button01);
		button3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog3.dismiss();
			}
		});
		dialog3.show();
	}

	/**
	 * Use this method to switch from an activity to another activity with
	 * Intent example usage: if you are on Activity1 and want to switch to
	 * Activity2, call: MethodHelper.switch_activity(Activity1.this,
	 * Activity2.class, ...)
	 * 
	 * @param from
	 *            an activity you are on
	 * @param to
	 *            a class you want to switch to
	 * @param animation
	 *            true if animation wants to be activated during activity
	 *            transition
	 */
	public static void switch_activity(Context from, Class<?> to,
			boolean animation) {
		Intent i = new Intent(from, to);
		if (!animation) {
			i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		}
		i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
		from.startActivity(i);
	}

	/**
	 * Use this method to switch from an activity to another activity with
	 * Intent example usage: if you are on Activity1 and want to switch to
	 * Activity2, call: MethodHelper.switch_activity(Activity1.this,
	 * Activity2.class, ...)
	 * 
	 * @param from
	 *            an activity you are on
	 * @param to
	 *            a class you want to switch to
	 * @param bundle
	 *            the bundle you want to pass to the next activity
	 * @param normalSound
	 *            boolean true if you want the normal_button sound to be played
	 */
	public static void switch_activity(Context from, Class<?> to,
			Bundle bundle, boolean normalSoundPlayed) {
		/*
		 * if (normalSoundPlayed) playSound(MediaPlayer.create(from,
		 * R.raw.normal_button));
		 */

		Intent i = new Intent(from, to);
		i.putExtras(bundle);
		i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		from.startActivity(i);
	}

	/**
	 * Use this method to switch from an activity to another activity with
	 * Intent example usage: if you are on Activity1 and want to switch to
	 * Activity2, call: MethodHelper.switch_activity(Activity1.this,
	 * Activity2.class, ...)
	 * 
	 * @param from
	 *            an activity you are on
	 * @param to
	 *            a class you want to switch to
	 * @param bundle
	 *            the bundle you want to pass to the next activity
	 * @param normalSound
	 *            boolean true if you want the normal_button sound to be played
	 */
	public static void switch_activity_wc(Context from, Class<?> to,
			boolean normalSoundPlayed) {
		/*
		 * if (normalSoundPlayed) playSound(MediaPlayer.create(from,
		 * R.raw.normal_button));
		 */

		Intent i = new Intent(from, to);
		from.startActivity(i);
	}

	/**
	 * Method to show that error occured when dealing with credentials
	 * 
	 * @param from
	 *            Activity you are in
	 * @param errorCount
	 *            the number of error occurs
	 */
	public static void errorOccured(Activity from, int errorCount) {
		// playSound(MediaPlayer.create(from, R.raw.fail_sign_in));
		if (errorCount < 2) {
			MethodHelper.showAlertBox(from, "Error",
					"Error occured. Please try again");
		} else {
			MethodHelper.showGetHelpToast(from,
					"Error occured. Please get an associate for help.");
		}
	}

	public static void showUserProfileDialog(final Activity from, Resources res) {
		final Dialog dialog3 = new Dialog(from);
		dialog3.setTitle(CurrentUser.getUsername());
		dialog3.setContentView(R.layout.user_profile_dialog);
		String temp = "";
		TextView text3 = (TextView) dialog3.findViewById(R.id.TextView01);
		ImageView iv1 = (ImageView) dialog3.findViewById(R.id.imageView1);
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			iv1.setImageResource(R.drawable.januardo);
			temp = "Hi, I'm Januardo Jankowski! I work a 9-5 desk job and make around $40,000 a year. The economy is worrying me, I don't know how long I can keep my job. Someone told me I can try the stock market for a bit of extra money, so maybe that will help me pay for my house in a few years. I'm new to the whole stocks deal, but I've heard it can make a lot of money! Although, I've also heard people lose a lot too. I'd like to make my way into the investing world, slowly and carefully.";
		} else {
			iv1.setImageResource(R.drawable.mary);
			temp = "Hey, I'm "
					+ CurrentUser.getUsername()
					+ "!  I'm the current owner and operator of the Canny Hotel, which has been in my family since the year 1337.  Impressive, I know, but we've fallen on hard times.  We're rundown, in debt, and we've tried everything.  I was reading our family history, though, and I came across emergency plans for what to do if the hotel is in trouble - play the stock market.  I can't be sure, but I have to take my shot to save the hotel!";
		}
		text3.setText(temp);
		Button button3 = (Button) dialog3.findViewById(R.id.Button01);
		button3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dialog3.dismiss();
				if (MainPageActivity.first_time) {
					MethodHelper.showAlertBox(from, "Objective "
							+ CurrentUser.objectiveNumber,
							CurrentUser.currentObj.getObjective());
					MainPageActivity.first_time = false;
				}
			}
		});
		dialog3.show();
	}

	/**
	 * Method to show edit dialog
	 * 
	 * @param a
	 *            activity you are on
	 * @return the loading dialog
	 */
	/*
	 * public static Dialog showEditDialog(Activity a, int type) { //Dialog
	 * mLoadingDialog = new Dialog(a, R.layout.loading_screen); Dialog
	 * mEditDialog = new Dialog(a);
	 * mEditDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); if (type ==
	 * EDIT_CHECKLIST) mEditDialog.setContentView(R.layout.edit_cl_dialog); else
	 * if (type == EDIT_TASK)
	 * mEditDialog.setContentView(R.layout.edit_task_dialog);
	 * mEditDialog.show(); return mEditDialog; }
	 */
}